feat(anthropic): support websearch tool #9407
Open
+282
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for Anthropic's Web Search tool (
web_search_20250305) to@langchain/anthropic, enabling Claude to perform real-time web searches and cite sources in its responses.Usage
Based on the direction from #9406 this would implement the websearch tool. Here is the example output from the example script:
{ messages: [ HumanMessage { "id": "c7416f2c-bea0-4e49-a3b6-384b23099211", "content": "What's the weather in NYC?", "additional_kwargs": {}, "response_metadata": {} }, AIMessage { "id": "msg_01CqUm4rR4KrgtUvemNA1gfK", "content": [ { "type": "server_tool_use", "id": "srvtoolu_01NottiD8nsS9Y4H64A1TEsb", "name": "web_search", "input": { "query": "NYC weather today" } }, { "type": "web_search_tool_result", "tool_use_id": "srvtoolu_01NottiD8nsS9Y4H64A1TEsb", "content": [ "[Object]", "[Object]", "[Object]", "[Object]", "[Object]", "[Object]", "[Object]", "[Object]", "[Object]", "[Object]" ] }, { "type": "text", "text": "Based on the latest weather information for New York City:\n\n**Today (Thursday, November 14):**\n" }, { "citations": [ "[Object]" ], "type": "text", "text": "Partly cloudy skies in the morning will give way to cloudy skies during the afternoon, with a high near 53°F and winds from the west-northwest at 10 to 15 mph." }, { "type": "text", "text": "\n\n**Tonight:**\n" }, { "citations": [ "[Object]" ], "type": "text", "text": "Mostly cloudy, then gradually becoming mostly clear, with a low around 42°F and west wind around 13 mph, with gusts as high as 24 mph." }, { "type": "text", "text": "\n\n**Recent Weather Note:**\n" }, { "citations": [ "[Object]" ], "type": "text", "text": "NYC saw its first snow of the season on Tuesday, with wintry showers during the morning commute." }, { "type": "text", "text": "\n\nThe weather is cool and breezy today with temperatures in the low 50s, typical for mid-November in New York City." } ], "name": "model", "additional_kwargs": { "model": "claude-sonnet-4-5-20250929", "id": "msg_01CqUm4rR4KrgtUvemNA1gfK", "type": "message", "role": "assistant", "stop_reason": "end_turn", "stop_sequence": null, "usage": { "input_tokens": 9422, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0, "cache_creation": { "ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 0 }, "output_tokens": 290, "service_tier": "standard", "server_tool_use": { "web_search_requests": 1 } } }, "response_metadata": { "model": "claude-sonnet-4-5-20250929", "id": "msg_01CqUm4rR4KrgtUvemNA1gfK", "stop_reason": "end_turn", "stop_sequence": null, "usage": { "input_tokens": 9422, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0, "cache_creation": { "ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 0 }, "output_tokens": 290, "service_tier": "standard", "server_tool_use": { "web_search_requests": 1 } }, "type": "message", "role": "assistant", "model_provider": "anthropic" }, "tool_calls": [], "invalid_tool_calls": [], "usage_metadata": { "input_tokens": 9422, "output_tokens": 290, "total_tokens": 9712, "input_token_details": { "cache_creation": 0, "cache_read": 0 } } } ] }